﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;

namespace FetchDataCompare
{
	public class GeoObjectStructBase
	{
		public GeoObjectBase GeoObject { get; set; }

		public string Attribute { get; set; }

		public Element UseElement { get; set; }

		public string GUID { get; set; }

		public Document UseDoc { get; set; }

		public string ViewName { get; set; }

		public View UseView { get; set; }

		public List<Element> SrcLstBoundary { get; set; }

		public List<ElementId> SrcLstBoundaryIDs { get; set; }

		public List<Element> DesLstBoundary { get; set; }

		public List<ElementId> DesLstBoundaryIDs { get; set; }

		public GeoObjectStructBase(Element element, View view)
		{
			this.SrcLstBoundary = new List<Element>();
			this.SrcLstBoundaryIDs = new List<ElementId>();
			this.DesLstBoundary = new List<Element>();
			this.DesLstBoundaryIDs = new List<ElementId>();
			this.UseElement = element;
			this.GUID = element.UniqueId;
			this.UseView = view;
			this.ViewName = view.Name;
			this.UseDoc = element.Document;
			this.GeoObject = this.CreateGeoObjectByElement(element);
		}

		public GeoObjectStructBase()
		{
			this.SrcLstBoundary = new List<Element>();
			this.SrcLstBoundaryIDs = new List<ElementId>();
			this.DesLstBoundary = new List<Element>();
			this.DesLstBoundaryIDs = new List<ElementId>();
		}

		public void InitGeoObj()
		{
			this.GUID = this.UseElement.UniqueId;
			this.ViewName = this.UseView.Name;
			this.UseDoc = this.UseElement.Document;
			this.GeoObject = this.CreateGeoObjectByElement(this.UseElement);
		}

		public override bool Equals(object obj)
		{
			GeoObjectStructBase geoObjectStructBase = obj as GeoObjectStructBase;
			return geoObjectStructBase != null && (this.GUID == geoObjectStructBase.GUID && this.Attribute == geoObjectStructBase.Attribute && this.GeoObject.Equals(geoObjectStructBase.GeoObject));
		}

		public void CreateGeoStructByElement(Element element, View view)
		{
			GeoObjectStructBase geoObjectStructBase = new GeoObjectStructBase(element, view);
			geoObjectStructBase.GeoObject = this.CreateGeoObjectByElement(element);
		}

		public virtual string GetElementAttribute()
		{
			string text = "";
			string name = this.UseElement.Category.Name;
			text = text + name + ";";
			Parameter parameter = this.UseElement.GetParameter(BuiltInParameter.ELEM_FAMILY_PARAM);
			Parameter parameter2 = this.UseElement.GetParameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM);
			if (parameter != null && parameter2 != null)
			{
				string name2 = this.UseDoc.GetElement(parameter.AsElementId()).Name;
				string name3 = this.UseDoc.GetElement(parameter2.AsElementId()).Name;
				text = text + name2 + ";";
				text = text + name3 + ";";
			}
			XMLOperator xmloperator = new XMLOperator();
			List<string> needCheckAttribute = xmloperator.GetNeedCheckAttribute(this.UseElement.Category.Id.IntegerValue.ToString());
			foreach (string paramName in needCheckAttribute)
			{
				string str = this.UseElement.GetParameter(paramName).AsValueString();
				text = text + str + ";";
			}
			return text;
		}

		public GeoObjectBase CreateGeoObjectByElement(Element element)
		{
			GeoObjectBase result = null;
			switch (this.GetElementGeoType(element))
			{
			case EGeoObjectType.eBoudingBoxObj:
				result = new BoundingBoxGeoObject(element);
				break;
			case EGeoObjectType.eDimObj:
				result = new DimGeoObject(element);
				break;
			case EGeoObjectType.eTextNoteObj:
				result = new TextNoteGeoObject(element);
				break;
			case EGeoObjectType.eWireObj:
				result = new WireGeoObj(element);
				break;
			}
			return result;
		}

		private EGeoObjectType GetElementGeoType(Element element)
		{
			if (element is Dimension)
			{
				return EGeoObjectType.eDimObj;
			}
			if (element is TextNote)
			{
				return EGeoObjectType.eTextNoteObj;
			}
			if (element is Wire)
			{
				return EGeoObjectType.eWireObj;
			}
			return EGeoObjectType.eBoudingBoxObj;
		}

		public void GetStartAndEndPoint(Curve curve, ref string attr)
		{
			string str = string.Concat(new string[]
			{
				curve.GetEndPoint(0).X.ToString("f2"),
				";",
				curve.GetEndPoint(0).Y.ToString("f2"),
				";",
				curve.GetEndPoint(0).Z.ToString("f2"),
				";"
			});
			attr = attr + str + ";";
			string str2 = string.Concat(new string[]
			{
				curve.GetEndPoint(1).X.ToString("f2"),
				";",
				curve.GetEndPoint(1).Y.ToString("f2"),
				";",
				curve.GetEndPoint(1).Z.ToString("f2"),
				";"
			});
			attr = attr + str2 + ";";
		}

		public void DrawOutLine(GraphicsStyle linstyle, bool IfSrc)
		{
			try
			{
                if ((int)this.UseView.ViewType == 4)
				{
					try
					{
						if (!this.GeoObject.BoundingRect.IsZeroRect())
						{
							XYZ basisX = (this.GeoObject.BoundingRect.RightTop - this.GeoObject.BoundingRect.LeftTop).Normalize();
							XYZ baseY = (this.GeoObject.BoundingRect.LeftBottom - this.GeoObject.BoundingRect.LeftTop).Normalize();
							Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(this.GeoObject.BoundingRect.LeftTop, basisX, baseY);
							SketchPlane sketchPlane = SketchPlane.Create(this.UseDoc, plane);
							ModelCurve modelCurve = this.UseDoc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.LeftBottom, this.GeoObject.BoundingRect.RightBottom), sketchPlane);
							modelCurve.LineStyle = linstyle;
							ModelCurve modelCurve2 = this.UseDoc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.RightBottom, this.GeoObject.BoundingRect.RightTop), sketchPlane);
							modelCurve2.LineStyle = linstyle;
							ModelCurve modelCurve3 = this.UseDoc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.RightTop, this.GeoObject.BoundingRect.LeftTop), sketchPlane);
							modelCurve3.LineStyle = linstyle;
							ModelCurve modelCurve4 = this.UseDoc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.LeftTop, this.GeoObject.BoundingRect.LeftBottom), sketchPlane);
							modelCurve4.LineStyle = linstyle;
							if (IfSrc)
							{
								this.SrcLstBoundary.Add(modelCurve);
								this.SrcLstBoundary.Add(modelCurve2);
								this.SrcLstBoundary.Add(modelCurve3);
								this.SrcLstBoundary.Add(modelCurve4);
								this.SrcLstBoundaryIDs.Add(modelCurve.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve2.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve3.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve4.Id);
							}
							else
							{
								this.DesLstBoundary.Add(modelCurve);
								this.DesLstBoundary.Add(modelCurve2);
								this.DesLstBoundary.Add(modelCurve3);
								this.DesLstBoundary.Add(modelCurve4);
								this.DesLstBoundaryIDs.Add(modelCurve.Id);
								this.DesLstBoundaryIDs.Add(modelCurve2.Id);
								this.DesLstBoundaryIDs.Add(modelCurve3.Id);
								this.DesLstBoundaryIDs.Add(modelCurve4.Id);
							}
						}
						goto IL_4D4;
					}
					catch (Exception)
					{
						goto IL_4D4;
					}
				}
				try
				{
					if (!this.GeoObject.BoundingRect.IsZeroRect())
					{
						DetailCurve detailCurve = this.UseDoc.Create.NewDetailCurve(this.UseView, Line.CreateBound(this.GeoObject.BoundingRect.LeftBottom, this.GeoObject.BoundingRect.RightBottom));
						detailCurve.LineStyle = linstyle;
						DetailCurve detailCurve2 = this.UseDoc.Create.NewDetailCurve(this.UseView, Line.CreateBound(this.GeoObject.BoundingRect.RightBottom, this.GeoObject.BoundingRect.RightTop));
						detailCurve2.LineStyle = linstyle;
						DetailCurve detailCurve3 = this.UseDoc.Create.NewDetailCurve(this.UseView, Line.CreateBound(this.GeoObject.BoundingRect.RightTop, this.GeoObject.BoundingRect.LeftTop));
						detailCurve3.LineStyle = linstyle;
						DetailCurve detailCurve4 = this.UseDoc.Create.NewDetailCurve(this.UseView, Line.CreateBound(this.GeoObject.BoundingRect.LeftTop, this.GeoObject.BoundingRect.LeftBottom));
						detailCurve4.LineStyle = linstyle;
						if (IfSrc)
						{
							this.SrcLstBoundary.Add(detailCurve);
							this.SrcLstBoundary.Add(detailCurve2);
							this.SrcLstBoundary.Add(detailCurve3);
							this.SrcLstBoundary.Add(detailCurve4);
							this.SrcLstBoundaryIDs.Add(detailCurve.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve2.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve3.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve4.Id);
						}
						else
						{
							this.DesLstBoundary.Add(detailCurve);
							this.DesLstBoundary.Add(detailCurve2);
							this.DesLstBoundary.Add(detailCurve3);
							this.DesLstBoundary.Add(detailCurve4);
							this.DesLstBoundaryIDs.Add(detailCurve.Id);
							this.DesLstBoundaryIDs.Add(detailCurve2.Id);
							this.DesLstBoundaryIDs.Add(detailCurve3.Id);
							this.DesLstBoundaryIDs.Add(detailCurve4.Id);
						}
					}
				}
				catch (Exception)
				{
				}
				IL_4D4:;
			}
			catch (Exception)
			{
			}
		}

		private View GetViewInOtherDoc(Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ViewType[] viewTypes = new ViewType[]
			{
				(ViewType)116,
				(ViewType)1,
				(ViewType)2,
				(ViewType)3,
				(ViewType)117
			};
			IEnumerable<Element> enumerable = from View item in filteredElementCollector.OfClass(typeof(View)).WhereElementIsNotElementType()
			where !(item is View3D) && !item.IsTemplate && viewTypes.Contains(item.ViewType)
			select item;
			View result = null;
			foreach (Element element in enumerable)
			{
				if (element.UniqueId == this.UseView.UniqueId)
				{
					result = (element as View);
				}
			}
			return result;
		}

		public void DrawOutLine(Document doc, GraphicsStyle linstyle, bool IfSrc)
		{
			View viewInOtherDoc = this.GetViewInOtherDoc(doc);
			try
			{
                if ((int)this.UseView.ViewType == 4)
				{
					try
					{
						if (!this.GeoObject.BoundingRect.IsZeroRect())
						{
							XYZ basisX = (this.GeoObject.BoundingRect.RightTop - this.GeoObject.BoundingRect.LeftTop).Normalize();
							XYZ baseY = (this.GeoObject.BoundingRect.LeftBottom - this.GeoObject.BoundingRect.LeftTop).Normalize();
							Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(this.GeoObject.BoundingRect.LeftTop, basisX, baseY);
							SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
							ModelCurve modelCurve = doc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.LeftBottom, this.GeoObject.BoundingRect.RightBottom), sketchPlane);
							modelCurve.LineStyle = linstyle;
							ModelCurve modelCurve2 = doc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.RightBottom, this.GeoObject.BoundingRect.RightTop), sketchPlane);
							modelCurve2.LineStyle = linstyle;
							ModelCurve modelCurve3 = doc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.RightTop, this.GeoObject.BoundingRect.LeftTop), sketchPlane);
							modelCurve3.LineStyle = linstyle;
							ModelCurve modelCurve4 = doc.Create.NewModelCurve(Line.CreateBound(this.GeoObject.BoundingRect.LeftTop, this.GeoObject.BoundingRect.LeftBottom), sketchPlane);
							modelCurve4.LineStyle = linstyle;
							if (IfSrc)
							{
								this.SrcLstBoundary.Add(modelCurve);
								this.SrcLstBoundary.Add(modelCurve2);
								this.SrcLstBoundary.Add(modelCurve3);
								this.SrcLstBoundary.Add(modelCurve4);
								this.SrcLstBoundaryIDs.Add(modelCurve.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve2.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve3.Id);
								this.SrcLstBoundaryIDs.Add(modelCurve4.Id);
							}
							else
							{
								this.DesLstBoundary.Add(modelCurve);
								this.DesLstBoundary.Add(modelCurve2);
								this.DesLstBoundary.Add(modelCurve3);
								this.DesLstBoundary.Add(modelCurve4);
								this.DesLstBoundaryIDs.Add(modelCurve.Id);
								this.DesLstBoundaryIDs.Add(modelCurve2.Id);
								this.DesLstBoundaryIDs.Add(modelCurve3.Id);
								this.DesLstBoundaryIDs.Add(modelCurve4.Id);
							}
						}
						goto IL_4A0;
					}
					catch (Exception)
					{
						goto IL_4A0;
					}
				}
				try
				{
					if (!this.GeoObject.BoundingRect.IsZeroRect())
					{
						DetailCurve detailCurve = doc.Create.NewDetailCurve(viewInOtherDoc, Line.CreateBound(this.GeoObject.BoundingRect.LeftBottom, this.GeoObject.BoundingRect.RightBottom));
						detailCurve.LineStyle = linstyle;
						DetailCurve detailCurve2 = doc.Create.NewDetailCurve(viewInOtherDoc, Line.CreateBound(this.GeoObject.BoundingRect.RightBottom, this.GeoObject.BoundingRect.RightTop));
						detailCurve2.LineStyle = linstyle;
						DetailCurve detailCurve3 = doc.Create.NewDetailCurve(viewInOtherDoc, Line.CreateBound(this.GeoObject.BoundingRect.RightTop, this.GeoObject.BoundingRect.LeftTop));
						detailCurve3.LineStyle = linstyle;
						DetailCurve detailCurve4 = doc.Create.NewDetailCurve(viewInOtherDoc, Line.CreateBound(this.GeoObject.BoundingRect.LeftTop, this.GeoObject.BoundingRect.LeftBottom));
						detailCurve4.LineStyle = linstyle;
						if (IfSrc)
						{
							this.SrcLstBoundary.Add(detailCurve);
							this.SrcLstBoundary.Add(detailCurve2);
							this.SrcLstBoundary.Add(detailCurve3);
							this.SrcLstBoundary.Add(detailCurve4);
							this.SrcLstBoundaryIDs.Add(detailCurve.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve2.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve3.Id);
							this.SrcLstBoundaryIDs.Add(detailCurve4.Id);
						}
						else
						{
							this.DesLstBoundary.Add(detailCurve);
							this.DesLstBoundary.Add(detailCurve2);
							this.DesLstBoundary.Add(detailCurve3);
							this.DesLstBoundary.Add(detailCurve4);
							this.DesLstBoundaryIDs.Add(detailCurve.Id);
							this.DesLstBoundaryIDs.Add(detailCurve2.Id);
							this.DesLstBoundaryIDs.Add(detailCurve3.Id);
							this.DesLstBoundaryIDs.Add(detailCurve4.Id);
						}
					}
				}
				catch (Exception)
				{
				}
				IL_4A0:;
			}
			catch (Exception)
			{
			}
		}
	}
}
